<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:gategory:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="gategoryList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="标签图标" align="center" prop="gateIcon">
        <template slot-scope="scope">
          <el-image v-if="scope.row.gateIcon" style="width: 50px; height: 50px" 
          :src="scope.row.gateIcon" :preview-src-list="[scope.row.gateIcon]" />
        </template>
      </el-table-column>
      <el-table-column label="标签简介" align="center" prop="intro" :show-overflow-tooltip="true"/>
      <el-table-column label="品类限制" align="center" prop="godLimit" :show-overflow-tooltip="true">
         <template slot-scope="scope">
          <span v-for="(v,k) in scope.row.godLimit" :key="k">
            {{v===0?'不限':getClassName(v)}} 、
          </span>
        </template>
      </el-table-column>
      <el-table-column label="如何获得" align="center" prop="content" :show-overflow-tooltip="true"/>
      <el-table-column label="已获得人数" align="center" prop="obtains" width="180" />
      <el-table-column label="启用/禁用" align="center" >
        <template slot-scope="scope">
            {{scope.row.status===0?'禁用':'启用'}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status===0" size="mini"  v-hasPermi="['system:gategory:remove']" 
          type="text" @click="handleDelete(scope.row,1,'启用')" >启用</el-button>
          <el-button v-else-if="scope.row.status===1"size="mini"  v-hasPermi="['system:gategory:remove']" 
          type="text" @click="handleDelete(scope.row,0,'禁用')" >禁用</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:gategory:edit']"
          >修改</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row,-1,'删除')"
            v-hasPermi="['system:gategory:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改大神标签对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="标签图标" prop="gateIcon">
          <SingleUpload :avatar="form.gateIcon" @emitSetUrl="setUrl" />
        </el-form-item>
        <el-form-item label="品类限制" prop="godLimit">
          <el-select v-model="form.godLimit" placeholder="请选择品类限制" :style="{width: '50%'}" multiple >
            <el-option label="不限" :value="0"></el-option>
            <el-option v-for="(item, index) in godClassList" :key="index" :label="item.className" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签简介" >
          <el-input v-model="form.intro" placeholder="请输入标签简介" />
        </el-form-item>
        <el-form-item label="如何获得" prop="content">
          <el-input type="textarea" v-model="form.content" placeholder="请输入如何获得" :rows="3"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGategory, getGategory, delGategory, addGategory, updateGategory, exportGategory } from "@/api/system/gategory";
import { godClassAll } from "@/api/system/class";
import SingleUpload from '@/components/Upload/SingleUpload'
export default {
  name: "Gategory",
   components:{
    SingleUpload
  },
  data() {
    return {
      godClassList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 大神标签表格数据
      gategoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gateIcon: undefined,
        limit: undefined,
        intro: undefined,
        content: undefined,
        createData: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        gateIcon: [
          { required: true, message: "标签图标不能为空", trigger: "blur" }
        ],
        godLimit: [
          { required: true, message: "品类限制不能为空", trigger: "blur" }
        ],
        intro: [
          { required: true, message: "标签简介不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "如何获得不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
        createData: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.loadGodClass()
  },
  methods: {
    loadGodClass(){
      godClassAll().then(response => {
        this.godClassList = response.data
      });
    },
    getClassName(id){
      for (let i = 0; i < this.godClassList.length; i++) {
        const element = this.godClassList[i];
        if(element.id === id){
          return element.className
        }
      }
    },
    setUrl(data){
      this.form.gateIcon = data.url
    },
    /** 查询大神标签列表 */
    getList() {
      this.loading = true;
      listGategory(this.queryParams).then(response => {
        this.gategoryList = response.rows;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        gateIcon: undefined,
        godLimit: [],
        intro: undefined,
        content: undefined,
        createBy: undefined,
        createData: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加大神标签";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGategory(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改大神标签";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateGategory(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addGategory(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row,status,text) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认'+text+'大神标签编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGategory(ids,status);
        }).then(() => {
          this.getList();
          this.msgSuccess(text+"成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有大神标签数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportGategory(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>